#Requires AutoHotkey v2.0

dictionary(word) { 
  url := "https://api.dictionaryapi.dev/api/v2/entries/en/" . word
  dic := ComObject("WinHttp.WinHttpRequest.5.1")
  dic.Open("GET", url, true)
  dic.SetTimeouts(0, 60000, 30000, 1000)
  dic.Send()
  try {
    dic.WaitForResponse()
    obj := JSON2.parse(dic.ResponseText)
  } catch {
    text := ""
    meanings := "`n" . OnlyInput.parameter
    sound := ""
    audio := ""

  } else {
    if (ObjOwnPropCount(Obj)=3) {
      text := ""
      meanings := "`n" . OnlyInput.parameter
      sound := ""
      audio := ""

    } else { 
      if (obj[1].phonetics.Length > 1) {
        try {
          text := obj[1].phonetics[2].text
        } catch{
          text := ""
        }

        try {
          sound := obj[1].phonetics[2].audio
        } catch {
          sound := ""
        }

        audio := text . "  <audio src=`"" . sound . "`" autoplay></audio>"
      
      } else if (obj[1].phonetics.Length = 1) {
        try {
          text := obj[1].phonetics[1].text
        } catch {
          text := ""
        }

        try {
          sound := obj[1].phonetics[1].audio
        } catch {
          sound := ""
        }

        audio := text . "  <audio src=`"" . sound . "`" autoplay></audio>"
        
      } else {
        text := ""
        sound := ""
        audio := ""
      }
      
      for i in obj[1].meanings {
        meanings .= "`n-`s" i.partOfSpeech
        for k in i.definitions {
          meanings .= "`n`t-`s" . k.definition
        }
      }
    }
  } 
      obj := {text: text, sound: sound, audio: audio, content: meanings}
      return obj
}